package com.wn.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.CardMapper;
import com.wn.mapper.ConsumptionRecordMapper;
import com.wn.mapper.UserMapper;
import com.wn.ov.ConsumOv;
import com.wn.ov.DraRecordOv;
import com.wn.ov.DruglistOv;
import com.wn.pojo.Card;
import com.wn.pojo.Cost;
import com.wn.pojo.DraRecord;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class CardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ConsumptionRecordMapper crm;

    //就诊卡办理
    public ServerResponse addCard(Card card,Integer id){
       //通过病人的姓名查找病人的id
        User patient = userMapper.getPatientByName(card.getPatientName());
        if(StringUtils.isEmpty(patient)){
            return new ServerResponse(ResponseCode.ERROR,"请先登记");
        }
        Card card1 = cardMapper.selectByPatientId(patient.getId());
        if(!StringUtils.isEmpty(card1)){
            return new ServerResponse(ResponseCode.ERROR,"不能重复办卡");
        }
        card.setPatientId(patient.getId());
        card.setCreateUserId(id);
        card.setCardNumber(Integer.parseInt((Math.random()+"").toString().substring(2,11)));
        int result = cardMapper.insertSelective(card);
        if(result > 0){
            //添加成功
            return new ServerResponse(ResponseCode.SUCCESS,"添加成功");
        }
        //添加失败
        return new ServerResponse(ResponseCode.ERROR,"添加失败");
    }

    //就诊卡充值
    public ServerResponse addCardBalance(Card card){
        int result = cardMapper.insertSelective(card);
        if(result > 0){
            //添加成功
            return new ServerResponse(ResponseCode.SUCCESS,"添加成功");
        }else{
            //添加失败
            return new ServerResponse(ResponseCode.ERROR,"添加失败");
        }

    }

    //分页查询就诊卡
    public ServerResponse getCardList(PageParam<Card> pageParam){
            if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
                //分页
                PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

                List<Card> cardList = cardMapper.superSearch(pageParam.getSuperSearchKeyWord());
                PageInfo<Card> cardPageInfo = new PageInfo<Card>(cardList);
                return new ServerResponse(ResponseCode.SUCCESS,cardPageInfo);
            }else{
                //分页
                PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
                List<Card> list = cardMapper.selectAll();
                PageInfo<Card> pageInfo = new PageInfo<Card>(list);
                return new ServerResponse(ResponseCode.SUCCESS,pageInfo);
            }
    }

    //锁定
    public ServerResponse updateCardStatus1(Card card) {
       //通过id查找卡
        Card card1 = cardMapper.selectById(card.getId());
        card1.setStatus(1);
        int i = cardMapper.updateStatus(card1);
        if(i > 0){
            //添加成功
            return new ServerResponse(ResponseCode.SUCCESS,"锁定成功");
        }else{
            //添加失败
            return new ServerResponse(ResponseCode.ERROR,"锁定失败");
        }
    }

    //解锁
    public ServerResponse updateCardStatus2(Card card) {
        //通过id查找卡
        Card card1 = cardMapper.selectById(card.getId());
        card1.setStatus(0);
        int i = cardMapper.updateStatus(card1);
        if(i > 0){
            //添加成功
            return new ServerResponse(ResponseCode.SUCCESS,"锁定成功");
        }else{
            //添加失败
            return new ServerResponse(ResponseCode.ERROR,"锁定失败");
        }
    }

    //充值
    public ServerResponse updateCardMoney(Card card) {
        //通过id来查找card
        Card card1 = cardMapper.selectById(card.getId());
        Card card2= new Card();
        card2.setId(card.getId());
        card2.setBalance(card1.getBalance().add(card.getBalance()));
        int i = cardMapper.updateByPrimaryKeySelective(card2);
        if(i > 0){
            //添加成功
            return new ServerResponse(ResponseCode.SUCCESS,"充值成功");
        }else{
            //添加失败
            return new ServerResponse(ResponseCode.ERROR,"充值失败");
        }
    }

    //查看消费记录
    public ServerResponse getCostOutDetailList(PageParam<ConsumOv> pageParam) {
        //通过Id来查找
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<ConsumOv> costOvList = crm.superSearch(pageParam.getSuperSearchKeyWord(), pageParam.getId());
            PageInfo<ConsumOv> costOvPageInfo = new PageInfo<ConsumOv>(costOvList);
            return new ServerResponse(ResponseCode.SUCCESS, costOvPageInfo);
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<ConsumOv> list = crm.getCardOutDetail(pageParam.getId());
            PageInfo<ConsumOv> pageInfo = new PageInfo<ConsumOv>(list);
            return new ServerResponse(ResponseCode.SUCCESS, pageInfo);
        }
    }
}
